package net.dgg.rz.production.service;

import net.dgg.iboss.base.util.JsonUtil;
import net.dgg.iboss.base.util.http.HttpUtility;
import net.dgg.iboss.base.util.http.ResultObj;
import net.dgg.rz.common.base.service.BaseService;
import net.dgg.rz.common.constant.ModuleCodeConstant;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.constant.SystemCommonConstant;
import net.dgg.rz.common.utils.DataPermissionUtils;
import net.dgg.rz.common.utils.SearchUtils;
import net.dgg.rz.production.dao.CompleteCustomerDao;
import net.dgg.rz.production.entity.dto.CompleteCustomerDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zyou on 2019/4/28.
 */
@Service
public class CompleteCustomerService extends BaseService {

    @Autowired
    private CompleteCustomerDao completeCustomerDao;
    @Autowired
    private DataPermissionUtils dataPermissionUtils;
    @Autowired
    private SearchUtils searchUtils;
    @Value("${busBaseUrl}")
    private String busBaseUrl;
    /**
     * 查询成单客户
     * @param params
     * @return
     */
    public List<CompleteCustomerDto> queryCompleteCustomerWithPage(Map params){
        //参数注入
        searchUtils.paramsComplete(params);
        //数据权限注入
        dataPermissionUtils.flowUserDataAccess(params, ModuleCodeConstant.RZSC_COMPLETE_CUSTOMER_CODE);
        List<Long>  userIds = completeCustomerDao.queryCompleteCustomerWithPage(params);
        return this.queryCustomerInfoByUserIds(userIds);
    }


    /**
     * 通过客户id查询客户信息,商机信息
     * @param customerId
     * @return
     */
    public CompleteCustomerDto queryCustomerInfoByCustomerId(Long customerId) {
        CompleteCustomerDto completeCustomerDto = null;
        try {
            //调取接口查询客户相关信息
            if(customerId != null){
                Map customerMap = new HashMap();
                customerMap.put("busTypeCode", ScProductConstant.BUS_YT_DK);
                customerMap.put("customerId",customerId);
                String res = HttpUtility.postRest(busBaseUrl + SystemCommonConstant.COMPLETE_CUSTOMER_INFO_ONE,customerMap);
                ResultObj resObj = JsonUtil.json2Obj(res, ResultObj.class);
                if (resObj.getCode() == 0 ) {
                    String resData = resObj.getData();
                    completeCustomerDto = JsonUtil.json2Obj(resData,CompleteCustomerDto.class);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return completeCustomerDto;
    }

    /**
     * 通过客户id集合查询客户信息
     * @param userIds
     * @return
     */
    public List<CompleteCustomerDto> queryCustomerInfoByUserIds(List<Long> userIds) {
        List<CompleteCustomerDto> completeCustomerDtos = new ArrayList<>();
        try {
            //调取接口查询客户相关信息
            if(userIds != null && userIds.size()>0){
                Map customerMap = new HashMap();
                customerMap.put("busTypeCode", ScProductConstant.BUS_YT_DK);
                customerMap.put("customerIds",userIds);
                String res = HttpUtility.postJson(busBaseUrl + SystemCommonConstant.COMPLETE_CUSTOMER_INFO,customerMap);
                ResultObj resObj = JsonUtil.json2Obj(res, ResultObj.class);
                if (resObj.getCode() == 0 ) {
                    String resData = resObj.getData();
                    completeCustomerDtos = JsonUtil.json2List(resData,CompleteCustomerDto.class);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return completeCustomerDtos;
    }

    /**
     * 今日系统使用情况数据
     */
    public Map getTodayUser() {
        return completeCustomerDao.getTodayUser();
    }

    /**
     * 根据客户id查询已放款的销售订单数据
     * @param params
     * @return
     */
    public List<Map> findOrderNoCustomerId(Map params) {
        //数据权限注入
        dataPermissionUtils.flowUserDataAccess(params, ModuleCodeConstant.RZSC_COMPLETE_CUSTOMER_CODE);
        return completeCustomerDao.findOrderNoCustomerId(params);
    }
}
